<template>
    <ul class="list" >
      <li class="item" v-for="item in letters" :key="item" @click="clickAlphabet" :ref="item"
          @touchstart="handleTouchStart"
          @touchmove = "handleTouchMove"
          @touchend = "handleTouchEnd"
      >{{item}}</li>
    </ul>
</template>

<script>
export default {
  props: {
    cities: Object
  },
  data () {
    return {
      touchFlag: false,
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    clickAlphabet: function (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.touchFlag = true
    },
    handleTouchMove (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        let clientY = e.touches[0].clientY - 89
        let index = Math.floor((clientY - this.startY) / 20)
        if (this.letters[index]) {
          this.$emit('change', this.letters[index])
        }
      }, 16)
    },
    handleTouchEnd () {
      this.touchFlag = false
    }
  },
  components: {
  },
  computed: {
    letters () {
      const cityArr = Object.keys(this.cities)
      return cityArr
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "~styles/varibles.styl"
  .list
    display :flex
    flex-direction: column
    justify-content: center
    top:1.78rem
    right :0
    bottom :0
    width :0.4rem
    position: absolute
    .item
      text-align: center
      line-height 0.4rem
      color :$bgColor
</style>
